//
//  ModelDelayTerm.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/18.
//

#import "ModelDelayTerm.h"

NSString *const kDelayTermDelayList = KEY_delayList;
NSString *const kDelayTermDefaultValue = KEY_defaultValue;

@interface ModelDelayTerm ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ModelDelayTerm

@synthesize delayList = _delayList;
@synthesize defaultValue = _defaultValue;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
        self.delayList = [self objectOrNilForKey:kDelayTermDelayList fromDictionary:dict];
        self.defaultValue = [[self objectOrNilForKey:kDelayTermDefaultValue fromDictionary:dict] doubleValue];
    }
    return self;
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    NSMutableArray *tempArrayForDelayList = [NSMutableArray array];
    for (NSObject *subArrayObject in self.delayList) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            [tempArrayForDelayList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            [tempArrayForDelayList addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForDelayList] forKey:kDelayTermDelayList];
    [mutableDict setValue:[NSNumber numberWithDouble:self.defaultValue] forKey:kDelayTermDefaultValue];
    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}

@end
